package com.mouse.hxsj.service;

import com.mouse.damo.micro.*;
import com.mouse.haoi.entity.Point;
import com.mouse.haoi.service.CustomService;
import com.mouse.hxsj.constants.ComPool;
import com.mouse.hxsj.constants.SystemParams;
import com.mouse.hxsj.entity.enums.MapPlace;
import com.mouse.hxsj.util.CommonUtil;
import com.mouse.hxsj.util.StringUtil;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class BaoService {
    static CustomService customService = new CustomService();
    boolean mianOpen = false;

    public void setMianOpen(boolean mianOpen) {
        this.mianOpen = mianOpen;
        this.shangGu = false;
    }

    @Setter
    Integer mainHwnd;
    @Setter
    List<Integer> otherHwndList;
    @Setter
    JButton button;
    boolean shangGu = false;

    public boolean mainWait() {
        List<Com> comList = ComPool.getComList();
        Com mainCom = comList.stream().filter(com -> mainHwnd.equals(com.getHwnd())).findFirst().orElse(null);
        if (mainCom == null) return false;
        Window window = new Window(mainCom);
        FindStr findStr = new FindStr(mainCom);
        Press press = new Press(mainCom);
        mainCom.setDict(0, "坐标.txt");
        mainCom.setDict(8, "妖魔.txt");
        while (true) {
            Sleep.sleep(50);
            if (!mianOpen) continue;
            int[] windowRect = window.getClientSize(mainHwnd);
            int height = windowRect[2], width = windowRect[1];
            int[] pics = findStr.findStrE(0, 0, width, height*3/4, "上古妖魔",
                    "95e200-000000", 0.9, 0);
            if (pics[0] == -1) continue;
            String mapStr = findStr.readStr(0, 0, width, height*3/4, "ff7e00-000000", "", 0.9, 8);
            log.info("开图：{}", mapStr);
            if (StringUtil.isBlank(mapStr)) continue;

            //关闭 音乐
            ding(mainCom);
            mianOpen = false;
            shangGu = true;
            button.setText("开启");
            MapPlace mapPlace = MapPlace.getByMap(mapStr);
            MapPlace.fly(mainCom, mapPlace);
            press.keyPress(Press.M);
            boolean flag = customService.loop(10, () -> CommonUtil.getMap(mainCom).getMap().equals(mapStr));
//            if (!flag) return false;
            //TODO 找图
            if (false) break;
        }
        return true;
    }

    public void otherFly() {
        List<Com> comList = ComPool.getComList();
        List<Com> otherComList = otherHwndList.stream().map(hwnd -> {
            return comList.stream().filter(com -> hwnd.equals(com.getHwnd())).findFirst().orElse(null);
        }).collect(Collectors.toList());
        Com mainCom = comList.stream().filter(com -> mainHwnd.equals(com.getHwnd())).findFirst().orElse(null);
        Window mainWindow = new Window(mainCom);
        Com otherCom = new Com();
        otherCom.setResourcePath(SystemParams.sourcePath);
        Mouse otherMouse = new Mouse(otherCom);
        FindPic otherFindPic = new FindPic(otherCom);
        SystemUtil systemUtil = new SystemUtil(mainCom);
        int wheight = systemUtil.getScreenHeight(), wwidth = systemUtil.getScreenWidth();
        while (true) {
            Sleep.sleep(50);
            if (!shangGu && !mianOpen) continue;
            boolean flag = false;
            int[] mousePoint = new int[3];
            for (Com com : otherComList) {
                if (com == null) continue;
                Sleep.sleep(50);
                FindPic findPic = new FindPic(com);
                Window window = new Window(com);
                int[] windowRect = window.getClientSize(com.getHwnd());
                int height = windowRect[2], width = windowRect[1];
                //TODO
                int[] pic1 = findPic.findPic(0, 0, width, height,
                        "bmp/跟飞.bmp", "203040", 0.8, 0);
                if (pic1[0] == -1) continue;
                mousePoint = otherMouse.getMousePoint();
                window.setWindowActivate(com.getHwnd());
                Point point = customService.loopPoint(3, () -> {
                    int[] pic = otherFindPic.findPic(0, 0, wwidth, wheight,
                            "bmp/跟飞.bmp", "203040", 0.8, 0);
                    return new Point(pic);
                });
                if (point.getState() == -1) continue;
                //TODO
                otherMouse.mouseClick(point.getX(), point.getY(), true);
                Sleep.sleep(50);
                otherMouse.mouseMoveTo(point.getX() + 200, point.getY() + 200);
                flag = true;
            }
            if (flag) {
                mainWindow.setWindowActivate(mainHwnd);
                if (mousePoint[0] != -1) {
                    otherMouse.mouseMoveTo(mousePoint[1], mousePoint[2]);
                }
            }
        }

    }

    private void ding(Com com) {
        java.io.File file = new java.io.File("dll\\ding.mp3");
        String absolutePath = file.getAbsolutePath();
        new File(com).play(absolutePath);
    }
}
